package developer.核心类.线程;

import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author zhangyongkang
 * @time 2025/7/23 11:11
 * @description
 */
public class ThreadDemo {
    public static void main(String[] args) throws InterruptedException {
        test2();
//        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor();
//        threadPoolExecutor.execute(() -> {
//        });
//
//        threadPoolExecutor.shutdown();
    }

    private static void test2() {
        Object a = new Object();
        Object b = new Object();
        new Thread(() -> {
            synchronized (a) {
                System.out.println("我拿到A了");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (b) {
                    System.out.printf("我要拿到B");
                }
            }
        }).start();

        new Thread(() -> {
            synchronized (b) {
                System.out.println("我拿到B了");
                synchronized (a) {
                    System.out.printf("我要拿到A");
                }
            }
        }).start();

    }

    private static void test1() throws InterruptedException {
        Thread thread = new Thread(() -> {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("被我阻塞了吧");
        });

        thread.setDaemon(true);
        thread.start();
        thread.join();
        System.out.println("我是主线程");
    }
}
